<?php

class ItemCategory extends Eloquent implements CrudableInterface
{
    public function itemTypes() {
        return $this->hasMany('ItemType');
    }

    public function group() {
        return $this->belongsTo('Group');
    }

    public function showTitle() 
    {
        return $this->name;
    }

    public function showFields()
    {
        return array (
            'ID' => $this->id,
            'Name' => $this->name,
        );
    }

    public static function indexColumns()
    {
        return array (
            'ID' => array (
                'type'=>'numeric',
                'field'=>'id'
            ),
            'Name' => array (
                'type'=>'string',
                'field'=>'name'
            ),
            'Pounds in Stock' => array (
                'type'=>'numeric',
                'field'=>function ($obj) { 
                    $total = 0;
                    foreach ($obj->itemTypes as $it) {
                        foreach ($it->lots as $lot) {
                            $total += $lot->items()->where('status', '=', 'in stock')->count() * $lot->pounds_per_item;
                        }
                    }
                    return $total;
                }
            ),
        );
    }

    public static function editFields($obj=null)
    {
        return array (
            'name' => array (
                'type'=>'text',
                'label'=>'Name',
            ),
        );
        
    }
}